package org.softmed.rest.admin.resource;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.softmed.rest.admin.interfaces.Recompiler;

import restlet.description.convert.RESTResourceDescription;


public class ResourceHandlerPath extends JPanel implements KeyListener,
		DocumentListener {

	JTextField handlerPath = new JTextField();
	int width = 540;
	int height = 25;
	Recompiler recompiler;
	private RESTResourceDescription resource;

	public ResourceHandlerPath() {

		handlerPath.addKeyListener(this);
		handlerPath.getDocument().addDocumentListener(this);

		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
//		setBackground(Color.RED);

		setPreferredSize(new Dimension(width, height));
		setMaximumSize(new Dimension(width, height));
		setMinimumSize(new Dimension(width, height));

		handlerPath.setPreferredSize(new Dimension(width-120, height));
		handlerPath.setMaximumSize(new Dimension(width-120, height));
		handlerPath.setMinimumSize(new Dimension(width-120, height));

		add(new JLabel("Resource Handler:"));
		add(handlerPath);
		add(Box.createHorizontalGlue());

	}

	public void setText(String string) {
		handlerPath.setText(string);

	}

	public String getText() {
		return handlerPath.getText();
	}

	public Recompiler getRecompiler() {
		return recompiler;
	}

	public void setRecompiler(Recompiler recompiler) {
		this.recompiler = recompiler;
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// handlerPath.setName(name.getText());
		safeRecompile();
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public JTextField getHandlerPath() {
		return handlerPath;
	}

	protected void safeRecompile() {
//		resource.setHandler(handlerPath.getText());
		if (recompiler != null)
			recompiler.recompile();
	}

	@Override
	public void changedUpdate(DocumentEvent arg0) {
		safeRecompile();

	}

	@Override
	public void insertUpdate(DocumentEvent arg0) {
		safeRecompile();

	}

	@Override
	public void removeUpdate(DocumentEvent arg0) {
		safeRecompile();

	}

	public void setResourceDescription(
			RESTResourceDescription resourceDescription) {
		this.resource = resourceDescription;
//		if (resourceDescription.getHandler() != null)
//			handlerPath.setText(resourceDescription.getHandler());
//		else
//			handlerPath.setText("");

	}

}
